package com.fandou.airobot.utils;


import android.os.Handler;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.blankj.utilcode.util.FragmentUtils;
import com.fandou.airobot.R;

/**
 * @author Jm
 * @date 2019/06/27
 */
public class MyFragmentUtilsWrapper {

    /**
     * 添加fragment，不带动画
     *
     * @param fragmentManager fragmentManager
     * @param fragment        fragment
     * @param contentId       contentId
     */
    public static void addWithOutAnim(FragmentManager fragmentManager, Fragment fragment, int contentId) {
        FragmentUtils.add(fragmentManager, fragment, contentId);
    }


    /**
     * 添加fragment，带动画效果，进厂动画，退出动画。isAddStack 为true，可以pop
     *
     * @param fragmentManager fragmentManager
     * @param fragment        fragment
     * @param contentId       contentId
     * @param singleton       是否为单例
     */
    public static void addFromBottom(FragmentManager fragmentManager, Fragment fragment, int contentId, boolean singleton) {
        if (singleton) {
            Fragment topInStack = FragmentUtils.getTopInStack(fragmentManager);
            if (topInStack == null) {
                addFromBottom(fragmentManager, fragment, contentId);
            } else {
                if (!topInStack.getClass().equals(fragment.getClass())) {
                    addFromBottom(fragmentManager, fragment, contentId);
                }
            }
        }else{
            addFromBottom(fragmentManager, fragment, contentId);
        }
    }

    public static void addFromBottom(FragmentManager fragmentManager, Fragment fragment, int contentId){
        FragmentUtils.add(fragmentManager, fragment, contentId, true, R.anim.bottom_in, R.anim.bottom_out, R.anim.bottom_in, R.anim.bottom_out);
    }


    /**
     * 延时400毫秒移除fragment
     * 注意：不要更改为300毫秒，300毫秒的话，在启动activity过程中，仍然会见到这个remove的过程
     *
     * @param fragment fragment
     */
    public static void removeDelay(final Fragment fragment) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                FragmentUtils.remove(fragment);
            }
        }, 400);
    }

    /**
     * Pop the current fragment at top delay 300 millisecond
     *
     * @param fragmentManager FragmentManager
     */
    public static void popDelay(final FragmentManager fragmentManager) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                FragmentUtils.pop(fragmentManager);
            }
        }, 300);
    }

    /**
     * Pop the current fragment
     *
     * @param fragmentManager FragmentManager
     */
    public static void pop(FragmentManager fragmentManager) {
        FragmentUtils.pop(fragmentManager);
    }
}
